package com.suning.sawp.service.impl.biz;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.nsfuaa.common.NsfuaaResult;
import com.suning.nsfuaa.employee.EmployeeService;
import com.suning.nsfuaa.employee.dto.Employee;
import com.suning.nsfuaa.roleauth.RoleAuthService;
import com.suning.nsfuaa.roleauth.dto.AuthMenuInfo;
import com.suning.nsfuaa.roleauth.dto.EmpAuth;
import com.suning.nsfuaa.roleauth.dto.EmpLoginExt;
import com.suning.rsf.consumer.ServiceLocator;
import com.suning.rsf.provider.annotation.Implement;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.dao.mainpushnew.MainPushTaskHqViewDao;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.remote.dto.ClerkInfo;
import com.suning.sawp.remote.dto.DjLoginInfo;
import com.suning.sawp.remote.dto.ResponseData;
import com.suning.sawp.remote.intf.StoreBizService;
import com.suning.sawp.service.impl.dao.ClerkCustRelDaoService;
import com.suning.sawp.service.util.DateUtils;

@Implement(contract = StoreBizService.class, implCode = "storeBizService") 
@Service("storeBizService")
public class StoreBizServiceImpl implements StoreBizService {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(StoreBizServiceImpl.class);

    /**
     * 店+APP在NSF-UUA中的系统编码
     */
    private static final String DJ_APP = "DJAPP";
    
    /**
     * 促销员角色ID，写死
     */
    public static final String PROMOTIONID = "00010002";
    
    public static final String USER_PREFIX_Y = "Y";// 营业员前缀

    public static final String USER_PREFIX_Z = "Z";// 促销员前缀
    
    @Autowired
    private MainPushTaskHqViewDao mainPushTaskHqViewDao;
    
    @Resource
    ClerkCustRelDaoService clerkCustRelDaoService;
    
    private EmployeeService employeeService = ServiceLocator.getService(EmployeeService.class, "");
    
    private RoleAuthService roleAuthService = ServiceLocator.getService(RoleAuthService.class, "");
    
    @Resource
    StoreManService storeManService;
    
    @Override
    public ResponseData<List<String>> queryMainPushCommCdsInStore(String storeCode, String categoryCode) {
        ResponseData<List<String>> resp = new ResponseData<List<String>>();
        try {
            String month = DateUtils.getThisMonth();
            List<String> commCds = mainPushTaskHqViewDao.queryCommCdsByCateCode(categoryCode, month);
            resp.setData(commCds);
        } catch (Exception e) {
            Object[] args = {storeCode, categoryCode};
            LOGGER.error("StoreBizService.queryMainPushCommCdsInStore error. params:{}", args, e);
            resp.setRetFlag(ResponseData.FAIL);
            resp.setErrorCode(ErrorCodeConstants.ERR_0002);
            resp.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }
        
        return resp;
    }

    @Override
    public ResponseData<ClerkInfo> queryClerkInfoByCustNo(String custNo, String storeCode) {
        ResponseData<ClerkInfo> resp = new ResponseData<ClerkInfo>();
        try {
            // 查询店员-客户关系表查询会员编码对应的工号
            String staffId = clerkCustRelDaoService.getStaffIdByCustNo(custNo, storeCode);
            
            if (StringUtils.isNotBlank(staffId)) {
                // 根据工号查询NSF-UUA角色列表
                boolean isInStore = false;
                String saleCompanyCode = "";
                String positionId = "";
                EmpAuth empAuth = roleAuthService.getEmpAuth(DJ_APP, staffId);
                if (null != empAuth && NsfuaaResult.SUCCESS == empAuth.getStatus() 
                        && CollectionUtils.isNotEmpty(empAuth.getEmpAuthList())) {
                    for (AuthMenuInfo menuInfo : empAuth.getEmpAuthList()) {
                        EmpLoginExt detail = menuInfo.getEmpLoginExt();
                        // 判断门店编码是否相同，如果相同则认为还在目标门店
                        if (storeCode.equals(detail.getOrgCode())) {
                            isInStore = true;
                            saleCompanyCode = detail.getCompanyCode();
                            positionId = detail.getPostionId();
                            break;
                        }
                    }
                }
                // 根据工号查询NSF-UUA店员基础信息
                if (isInStore) {
                    Employee employee = employeeService.queryEmployeeBasicInfo(staffId);
                    if (null != employee) {
                        ClerkInfo clerkInfo = new ClerkInfo();
                        clerkInfo.setStaffId((PROMOTIONID.equals(positionId) ? USER_PREFIX_Z : USER_PREFIX_Y) + staffId);
                        clerkInfo.setStaffName(employee.getEmployeeName());
                        clerkInfo.setCompanyMobile(employee.getCompanyCellphone());
                        clerkInfo.setPersonMobile(employee.getPersonalCellphone());
                        clerkInfo.setSaleCompanyCode(saleCompanyCode);
                        resp.setData(clerkInfo);
                    } 
                }
            }
            
        } catch (Exception e) {
            Object[] args = {custNo, storeCode};
            LOGGER.error("StoreBizService.queryClerkInfoByCustNo error, params:{}", args, e);
            resp.setRetFlag(ResponseData.FAIL);
            resp.setErrorCode(ErrorCodeConstants.ERR_0002);
            resp.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }
        return resp;
    }

    @Override
    public ResponseData<DjLoginInfo> queryDjLoginInfoByStaffId(String staffId) {
        ResponseData<DjLoginInfo> resp = new ResponseData<DjLoginInfo>();
        try {
            // 查询当前用户的缓存信息
            StoreManDto manDto = storeManService.queryStoreManAttachInfo(staffId);
            if (null != manDto) {
                DjLoginInfo djLoginInfo = new DjLoginInfo();
                djLoginInfo.setStaffId(manDto.getStaffId());
                djLoginInfo.setPositionId(manDto.getPositionId());
                djLoginInfo.setStoreCode(manDto.getStoreCode());
                resp.setData(djLoginInfo);
            }
        } catch (Exception e) {
            LOGGER.error("queryDjLoginInfoByStaffId ERROR. param:{}", staffId, e);
            resp.setRetFlag(ResponseData.FAIL);
            resp.setErrorCode(ErrorCodeConstants.ERR_0002);
            resp.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }
        
        return resp;
    }

}
